﻿using System;
using System.Configuration;
using System.Linq;
using Quartz;
using ZilLion.Core.Log;
using ZilLion.TaskManager.Entity.Entity;
using ZilLion.TaskManager.Repository;

namespace ZilLion.Core.TaskManager.Client.Quartz
{
    /// <summary>
    ///     自定义触发器监听
    /// </summary>
    public class CustomTriggerListener : ITriggerListener
    {
        private readonly TaskStateRespository _taskStateRespository;

        public CustomTriggerListener(TaskConfig config)
        {
            _taskStateRespository = new TaskStateRespository();
            Name = config.TaskModule;
        }


        public string Name { get; }

        /// <summary>
        ///     Trigger被激发 它关联的job即将被运行
        /// </summary>
        /// <param name="trigger">触发器</param>
        /// <param name="context">上下文</param>
        public void TriggerFired(ITrigger trigger, IJobExecutionContext context)
        {
            ZilLionLogManager.GetLogger()
                .Trace($"{context.JobDetail.JobType.Name}在{DateTime.Now:yyyy-MM-dd HH:mm:ss} Trigger被激发 它关联的job即将被运行");
        }

        /// <summary>
        ///     Trigger被激发 它关联的job即将被运行,先执行(1)，在执行(2) 如果返回TRUE 那么任务job会被终止
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        bool ITriggerListener.VetoJobExecution(ITrigger trigger, IJobExecutionContext context)
        {
            if (context.NextFireTimeUtc != null)
            {
                var lastRunTime = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime,
                    TimeZoneInfo.Local);
            }
            var taskid = trigger.JobKey.Name;

            return false;
        }

        /// <summary>
        ///     错过触发时调用
        /// </summary>
        /// <param name="trigger">触发器</param>
        void ITriggerListener.TriggerMisfired(ITrigger trigger)
        {
            ZilLionLogManager.GetLogger().Warn($"{trigger.JobKey.Name}在{DateTime.Now:yyyy-MM-dd HH:mm:ss}错过触发！");
        }

        /// <summary>
        ///     Job完成时调用
        /// </summary>
        /// <param name="trigger">触发器</param>
        /// <param name="context">上下文</param>
        /// <param name="triggerInstructionCode"></param>
        void ITriggerListener.TriggerComplete(ITrigger trigger, IJobExecutionContext context,
            SchedulerInstruction triggerInstructionCode)
        {
            if (context.NextFireTimeUtc == null) return;
            var nextRunTime = TimeZoneInfo.ConvertTimeFromUtc(context.NextFireTimeUtc.Value.DateTime,
                TimeZoneInfo.Local);
            var taskid = trigger.JobKey.Name;

            #region 更新状态表

            var nodeid = ConfigurationManager.AppSettings["NodeId"];
            var runlogresponse = _taskStateRespository.GetTaskStateById(taskid, nodeid);

            if ((runlogresponse != null) && (runlogresponse.TaskStates != null))
            {
                var first = runlogresponse.TaskStates.FirstOrDefault();
                {
                    if (first != null)
                    {
                        first.Tasknextruntime = nextRunTime;
                        first.Tasklastruntime = DateTime.Now;
                        _taskStateRespository.UpdateTaskState(first);
                    }
                }
            }

            #endregion
        }
    }
}